package demo09StackQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:咭咭熊
 * Date:2021 - 10 - 24
 * Time:9:38
 */
//使用单链表实现队列
class Node{
    public int val;
    public Node next;

    public Node(int val){
        this.val = val;
    }
}
public class MyQueue {
    public Node first;
    public Node last;

    /***
     * 1.添加元素
     * @param val
     * @return
     */
    public boolean offer(int val){
        Node node = new Node(val);
        //第一次插入节点
        if(this.first == null){
            this.first = node;
            this.last = node;
        }else{
            this.last.next = node;
            this.last = node;
        }
        return true;
    }

    /***
     * 2.弹出一个元素
     * @return
     */
    public int poll(){
        if(isEmpty()){
            throw new RuntimeException("队列为空！");
        }
        int ret = this.first.val;
        this.first = this.first.next;
        return ret;
    }

    /***
     * 3.弹出队头元素
     * @return
     */
    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("队列为空！");
        }
        int ret = this.first.val;
        return ret;
    }

    /***
     * 4.判空
     * @return
     */
    public boolean isEmpty(){
        if(this.last == null &&this.first == null){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.offer(1);
        myQueue.offer(2);
        myQueue.offer(3);
        System.out.println(myQueue.peek());

    }
}
